package com.upcode.weighing.admin.controller;

import cn.hutool.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/graceful/registry-service")
public class GracefulOffline {

    private final static String ADMIN_NAME = "zhjc";
    private final static String PWD = "zhjc";

    @Autowired
    private EurekaAutoServiceRegistration eurekaAutoServiceRegistration;

    @PostMapping("/online")
    public String online(@RequestBody JSONObject jsonObject) {
        String admin = jsonObject.getStr("admin");
        String pwd = jsonObject.getStr("pwd");
        if (ADMIN_NAME.equals(admin) && PWD.equals(pwd)) {
            this.eurekaAutoServiceRegistration.start();
            return "execute online method, online success.";
        }
        return "auth method fail";
    }

    @PostMapping("/offline")
    public String offline(@RequestBody JSONObject jsonObject) {

        String admin = jsonObject.getStr("admin");
        String pwd = jsonObject.getStr("pwd");
        if (ADMIN_NAME.equals(admin) && PWD.equals(pwd)) {
            this.eurekaAutoServiceRegistration.stop();
            return "execute offline method, offline success.";
        }
        return "auth method fail";
    }
}